//
//  Spiel3ViewController.m
//  Lextra Junior ueb IT
//
//  Created by Dirk Lieder on 30.05.11.
//  Copyright 2011 Phosphon. All rights reserved.
//

#import "Spiel3ViewController.h"
#import "AsyncImageView.h"
#import "getFilenameHD.h"

#import "Lextra_Engine.h"

#import <stdlib.h>
#import <time.h>

@implementation Spiel3ViewController


@synthesize outCountLabel;
@synthesize resetButton;
@synthesize outButton;
@synthesize gameButton1,gameButton2,gameButton3,gameButton4,gameButton5;

@synthesize richtigLabel,falschLabel;
@synthesize bildView;
@synthesize frLabel,deLabel;
@synthesize playButtonB1,playButtonB2;


@synthesize view1,view2;

static NSMutableArray *myArray;
static int myArrayPtr;
//___NGUYEN_HUY_SON___IPAD_ONLY______________________________________________________________________
@synthesize navigationBar;
@synthesize woerView;
@synthesize label1;
@synthesize buttonNewWord;
//END_NGUYEN_HUY_SON______________________________________________________________________

//__NGUYEN_HUY_SON_________________________________________________________________________
//___END_NGUYEN_HUY_SON________________________________________________________________________
static NSMutableArray *questArray;
static int questArrayPtr;

static int allQuestions, rightQuestions;

AVAudioPlayer *audioPlayer;
- (BOOL)isPadPortrait
{
    
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
            && (self.interfaceOrientation == UIInterfaceOrientationPortrait
                || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}


- (BOOL)isPadLandscape
{
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
            && (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight
                || self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft));
}
- (void)setDelegatePlud:(id)new_delegate{
	Delegate = new_delegate;
    
    // reset Game
    
    if(myArray) [myArray dealloc];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //Neu iPad
    {
        myArray = [[NSMutableArray alloc] initWithArray:[woerView getGameListPoinds] copyItems:YES]; 
    }
    else
    {
        myArray = [[NSMutableArray alloc] initWithArray:[(Lextra_Engine*)Delegate getGameListPoinds] copyItems:YES]; 
    }

    //myArray = [Delegate getGameListPoinds];
    
    NSMutableArray *tmpArray = [[NSMutableArray alloc] init];

    int i=[myArray count];
    while (i>0) {
        i--;
        int tmpObj = arc4random() % ([myArray count]);
        
        [tmpArray addObject:[myArray objectAtIndex:tmpObj]];
        [myArray removeObjectAtIndex:tmpObj];        
    }
    myArrayPtr = 0;
    myArray = tmpArray;
    
    allQuestions = 0;
    rightQuestions = 0;
    
    [self newQuest];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //Neu iPad
    {
        if ([self isPadPortrait] == TRUE) {
            outButton.frame = CGRectMake((768 - gameButton1.frame.size.width) /2, 285, gameButton1.frame.size.width, gameButton1.frame.size.height);
            gameButton1.frame = CGRectMake((768 - gameButton1.frame.size.width) /2, 345, gameButton1.frame.size.width, gameButton1.frame.size.height);
            gameButton2.frame = CGRectMake((768 - gameButton2.frame.size.width) /2, 391, gameButton2.frame.size.width, gameButton2.frame.size.height);
            gameButton3.frame = CGRectMake((768 - gameButton3.frame.size.width) /2, 437, gameButton3.frame.size.width, gameButton3.frame.size.height);
            gameButton4.frame = CGRectMake((768 - gameButton4.frame.size.width) /2, 483, gameButton4.frame.size.width, gameButton4.frame.size.height);
            gameButton5.frame = CGRectMake((768 - gameButton5.frame.size.width) /2, 529, gameButton5.frame.size.width, gameButton5.frame.size.height);
            label1.frame = CGRectMake((768-label1.frame.size.width)/2, 578, label1.frame.size.width, label1.frame.size.height);
            
            bildView.frame= CGRectMake((768 - bildView.frame.size.width - 10)/2 ,120 , bildView.frame.size.width, bildView.frame.size.height);;
            buttonNewWord.frame = CGRectMake((768-buttonNewWord.frame.size.width)/2,609 , buttonNewWord.frame.size.width, buttonNewWord.frame.size.height);
            deLabel.frame = CGRectMake((768-deLabel.frame.size.width)/2,577 , deLabel.frame.size.width, deLabel.frame.size.height);
            playButtonB1.frame = CGRectMake((768-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
            playButtonB2.frame = CGRectMake((768-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
            frLabel.frame = CGRectMake((768-frLabel.frame.size.width)/2,132 , frLabel.frame.size.width, frLabel.frame.size.height);
            richtigLabel.frame = CGRectMake((768-richtigLabel.frame.size.width)/2,90 , richtigLabel.frame.size.width, richtigLabel.frame.size.height);
            falschLabel.frame = CGRectMake((768 - falschLabel.frame.size.width - 10)/2 ,90 , falschLabel.frame.size.width, falschLabel.frame.size.height);

        }
        else
        {
            outButton.frame = CGRectMake((703 - gameButton1.frame.size.width) /2, 285, gameButton1.frame.size.width, gameButton1.frame.size.height);
            gameButton1.frame = CGRectMake((703 - gameButton1.frame.size.width) /2, 345, gameButton1.frame.size.width, gameButton1.frame.size.height);
            gameButton2.frame = CGRectMake((703 - gameButton2.frame.size.width) /2, 391, gameButton2.frame.size.width, gameButton2.frame.size.height);
            gameButton3.frame = CGRectMake((703 - gameButton3.frame.size.width) /2, 437, gameButton3.frame.size.width, gameButton3.frame.size.height);
            gameButton4.frame = CGRectMake((703 - gameButton4.frame.size.width) /2, 483, gameButton4.frame.size.width, gameButton4.frame.size.height);
            gameButton5.frame = CGRectMake((703 - gameButton5.frame.size.width) /2, 529, gameButton5.frame.size.width, gameButton5.frame.size.height);
            label1.frame = CGRectMake((703-label1.frame.size.width)/2, 578, label1.frame.size.width, label1.frame.size.height);
            
            bildView.frame= CGRectMake((703 - bildView.frame.size.width - 10)/2 ,120 , bildView.frame.size.width, bildView.frame.size.height);;
            buttonNewWord.frame = CGRectMake((703-buttonNewWord.frame.size.width)/2,609 , buttonNewWord.frame.size.width, buttonNewWord.frame.size.height);
            deLabel.frame = CGRectMake((703-deLabel.frame.size.width)/2,577 , deLabel.frame.size.width, deLabel.frame.size.height);
            playButtonB1.frame = CGRectMake((703-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
            playButtonB2.frame = CGRectMake((703-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
            frLabel.frame = CGRectMake((703-frLabel.frame.size.width)/2,132 , frLabel.frame.size.width, frLabel.frame.size.height);
            richtigLabel.frame = CGRectMake((703-richtigLabel.frame.size.width)/2,90 , richtigLabel.frame.size.width, richtigLabel.frame.size.height);
            falschLabel.frame = CGRectMake((768 - falschLabel.frame.size.width - 10)/2 ,90 , falschLabel.frame.size.width, falschLabel.frame.size.height);


        }
    }
}

- (void)newQuest{
    
    self.view = view1;
    
    [self stop:NULL];
    
    if(allQuestions == 0){
        resetButton.hidden = true;
        outCountLabel.hidden = true;
    } else {
        resetButton.hidden = false;
        outCountLabel.hidden = false;
        
        NSString *outText = [NSString stringWithFormat:@"%i von %i richtig gelöst",rightQuestions,allQuestions];
        outCountLabel.text = outText;
    }
    
    //if(questArray) [questArray dealloc];
    questArray = [[NSMutableArray alloc] init];
   
    // get 5 new questions
    int i=5;
    while (i>0) {
        i--;
        
        [questArray addObject:[myArray objectAtIndex:myArrayPtr]];
        
        myArrayPtr++;
        if(myArrayPtr >=[myArray count]) myArrayPtr = 0;
    }
    
    questArrayPtr = arc4random() % 5;
    
    // label buttons
    NSString *tmpString;
    tmpString = [self getWithArtikel:[questArray objectAtIndex:0]];
    [gameButton1 setTitle:tmpString forState:UIControlStateNormal];
    [gameButton1 setTitle:tmpString forState:UIControlStateSelected];
    [gameButton1 setTitle:tmpString forState:UIControlStateHighlighted];
   
    tmpString = [self getWithArtikel:[questArray objectAtIndex:1]];
    [gameButton2 setTitle:tmpString forState:UIControlStateNormal];
    [gameButton2 setTitle:tmpString forState:UIControlStateSelected];
    [gameButton2 setTitle:tmpString forState:UIControlStateHighlighted];
    
    tmpString = [self getWithArtikel:[questArray objectAtIndex:2]];
    [gameButton3 setTitle:tmpString forState:UIControlStateNormal];
    [gameButton3 setTitle:tmpString forState:UIControlStateSelected];
    [gameButton3 setTitle:tmpString forState:UIControlStateHighlighted];

    tmpString = [self getWithArtikel:[questArray objectAtIndex:3]];
    [gameButton4 setTitle:tmpString forState:UIControlStateNormal];
    [gameButton4 setTitle:tmpString forState:UIControlStateSelected];
    [gameButton4 setTitle:tmpString forState:UIControlStateHighlighted];

    tmpString = [self getWithArtikel:[questArray objectAtIndex:4]];
    [gameButton5 setTitle:tmpString forState:UIControlStateNormal];
    [gameButton5 setTitle:tmpString forState:UIControlStateSelected];
    [gameButton5 setTitle:tmpString forState:UIControlStateHighlighted];

    
    NSMutableString *tmpString2;
    if([[[NSMutableString alloc] initWithString:[[questArray objectAtIndex:questArrayPtr] objectForKey:@"origDE"]] length] !=0){
        tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:questArrayPtr] objectForKey:@"origDE"]];
    } else {
        tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:questArrayPtr] objectForKey:@"bezeichnungDE"]];
    }
    [outButton setTitle:tmpString2 forState:UIControlStateNormal];
    [outButton setTitle:tmpString2 forState:UIControlStateSelected];
    [outButton setTitle:tmpString2 forState:UIControlStateHighlighted];
    
    //NSLog(@"++%@", questArray);
    
}
- (IBAction) game1:(id)sender{
    [self showResult:questArrayPtr == 0];
}
- (IBAction) game2:(id)sender{
    [self showResult:questArrayPtr == 1];
}
- (IBAction) game3:(id)sender{
    [self showResult:questArrayPtr == 2];
}
- (IBAction) game4:(id)sender{
    [self showResult:questArrayPtr == 3];
}
- (IBAction) game5:(id)sender{
    [self showResult:questArrayPtr == 4];
}

- (IBAction) naechsteswort:(id)sender{
    [self newQuest];
}

- (IBAction) reset:(id)sender{
    
    [self stop:NULL];

    allQuestions = 0;
    rightQuestions = 0;
    
    [self newQuest];
}


- (void)showResult:(Boolean)result{
    
    self.view = view2;
   
    [self stop:NULL]; // stop sound
    
    // auswertung
    allQuestions++;
    if(result){
        rightQuestions++;
        richtigLabel.hidden = false;
        falschLabel.hidden = true;
        
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"richtig" ofType:@"m4a"]]; 
        if(audioPlayer) {
            [audioPlayer stop];
            [audioPlayer release];
        }
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [audioPlayer play];

    } else {
        richtigLabel.hidden = true;
        falschLabel.hidden = false;
        
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"falsch" ofType:@"m4a"]]; 
        if(audioPlayer) {
            [audioPlayer stop];
            [audioPlayer release];
        }
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [audioPlayer play];

    }
    
  
    // show bild
    NSString *myName = [[questArray objectAtIndex:questArrayPtr] objectForKey:@"bild"];
    NSString *path;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        path = [PHOSPHON imagePost:myName imagePostfix:@"_m@2x"];
    }
    else
    {
        path = [PHOSPHON imagePost:myName imagePostfix:@"_m"];
    }

	UIImage *img = [[UIImage imageNamed:path] retain]; 
	[bildView setImage: img];

    // show text
    NSString *tmpString1;
    tmpString1 =  [self getWithArtikel:[questArray objectAtIndex:questArrayPtr]];
        
    NSMutableString *tmpString2;
    if([[[questArray objectAtIndex:questArrayPtr] objectForKey:@"origDE"] length] !=0){
        tmpString2 = [[questArray objectAtIndex:questArrayPtr] objectForKey:@"origDE"];
    } else {
        tmpString2 = [[questArray objectAtIndex:questArrayPtr] objectForKey:@"bezeichnungDE"];
    }

    frLabel.text = tmpString1;
    deLabel.text = tmpString2;
    
    
}





-(NSString*) getWithArtikel:(NSDictionary*)inDic{

    NSMutableString *tmpString = [[NSMutableString alloc] initWithFormat:@"%@", [inDic objectForKey:@"artikel"]];
    
    if([tmpString length] > 0){
        if(![tmpString hasSuffix:@"'"]){
            [tmpString appendString:@" "];
        }
    }
    
    if([[inDic objectForKey:@"origFR"] length] !=0){
        [tmpString appendString:[inDic objectForKey:@"origFR"]]; 
    } else {
        [tmpString appendString:[inDic objectForKey:@"bezeichnungFR"]]; 
    }

    return tmpString;
}


- (IBAction) play:(id)sender{
	
    // first test
    NSString *myFilename;
    NSString *myName = [[questArray objectAtIndex:questArrayPtr] objectForKey:@"ton"];
    
    if([myName hasSuffix:@".mp3"]){
        myFilename = [myName substringToIndex:[myName length]-4];
    } else {
        myFilename = [[NSString alloc] initWithString:myName];
    }
    
    //NSLog(@"--%@", myFilename);
    
    if(myName){
        //NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:myFilename ofType:@"m4a"]]; 
        NSURL *url = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:myFilename ofType:@"m4a"]];
        if(audioPlayer) {
            [audioPlayer stop];
            [audioPlayer release];
        }
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        audioPlayer.delegate = self;
        [audioPlayer play];
        [url release];
    }
    
    playButtonB1.hidden = true;
    playButtonB2.hidden = false;

}

- (IBAction) stop:(id)sender{
    
    if(audioPlayer) [audioPlayer stop];
    playButtonB1.hidden = false;
    playButtonB2.hidden = true;
   
}

- (void)audioPlayerDidFinishPlaying:IntroPlayer successfully: (BOOL)flag
{
    if(audioPlayer) [audioPlayer stop];
    playButtonB1.hidden = false;
    playButtonB2.hidden = true;

}
- (void)sstop{
    if(audioPlayer) [audioPlayer stop];
    playButtonB1.hidden = false;
    playButtonB2.hidden = true;
    
    self.view = view1;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        srandom(time(NULL));
    }
    return self;
}

- (void)dealloc
{
    //__NGUYEN__HUY_SON
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        CGFloat navBarHeight = 49.0f;    
        CGRect frame = CGRectMake(navigationBar.frame.origin.x, navigationBar.frame.origin.y, navigationBar.frame.size.width, navBarHeight);
        [navigationBar setFrame:frame];
    }  //___END_NGUYEN_HUY_SON

    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    //__NGUYEN__HUY_SON
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        [navigationBar setTintColor:[[UIColor alloc] initWithRed:20.0 / 255 green:59.0 / 255 blue:102.0 / 255 alpha:1.0]];

        //navIndex.navigationBar.tintColor = [UIColor blueColor];
        CGFloat navBarHeight = 44.0f;    
        CGRect frame = CGRectMake(navigationBar.frame.origin.x, navigationBar.frame.origin.y, navigationBar.frame.size.width, navBarHeight);
        [navigationBar setFrame:frame];
    }  //___END_NGUYEN_HUY_SON

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

//__NGUYEN_HUY_SON______________________________________________________________________
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        return YES;
    }
    else
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
}
//___END_NGUYEN_HUY_SON_________________________________________________________________________

//______NGUYEN_HUY_SON___________________________________________________________________________________________
#pragma mark -
#pragma mark Managing the popover

- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Add the popover button to the left navigation item.
    [navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO];
    
    outButton.frame = CGRectMake((768 - gameButton1.frame.size.width) /2, 285, gameButton1.frame.size.width, gameButton1.frame.size.height);

    gameButton1.frame = CGRectMake((768 - gameButton1.frame.size.width) /2, 345, gameButton1.frame.size.width, gameButton1.frame.size.height);
    gameButton2.frame = CGRectMake((768 - gameButton2.frame.size.width) /2, 391, gameButton2.frame.size.width, gameButton2.frame.size.height);
    gameButton3.frame = CGRectMake((768 - gameButton3.frame.size.width) /2, 437, gameButton3.frame.size.width, gameButton3.frame.size.height);
    gameButton4.frame = CGRectMake((768 - gameButton4.frame.size.width) /2, 483, gameButton4.frame.size.width, gameButton4.frame.size.height);
    gameButton5.frame = CGRectMake((768 - gameButton5.frame.size.width) /2, 529, gameButton5.frame.size.width, gameButton5.frame.size.height);
    label1.frame = CGRectMake((768-label1.frame.size.width)/2, 578, label1.frame.size.width, label1.frame.size.height);
    
    bildView.frame= CGRectMake((768 - bildView.frame.size.width - 10)/2 ,120 , bildView.frame.size.width, bildView.frame.size.height);;
    buttonNewWord.frame = CGRectMake((768-buttonNewWord.frame.size.width)/2,609 , buttonNewWord.frame.size.width, buttonNewWord.frame.size.height);
    deLabel.frame = CGRectMake((768-deLabel.frame.size.width)/2,577 , deLabel.frame.size.width, deLabel.frame.size.height);
    playButtonB1.frame = CGRectMake((768-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
    playButtonB2.frame = CGRectMake((768-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
    frLabel.frame = CGRectMake((768-frLabel.frame.size.width)/2,132 , frLabel.frame.size.width, frLabel.frame.size.height);
    richtigLabel.frame = CGRectMake((768-richtigLabel.frame.size.width)/2,90 , richtigLabel.frame.size.width, richtigLabel.frame.size.height);
    falschLabel.frame = CGRectMake((768 - falschLabel.frame.size.width - 10)/2 ,90 , falschLabel.frame.size.width, falschLabel.frame.size.height);


}


- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Remove the popover button.
    [navigationBar.topItem setLeftBarButtonItem:nil animated:NO];
    
    outButton.frame = CGRectMake((703 - gameButton1.frame.size.width) /2, 285, gameButton1.frame.size.width, gameButton1.frame.size.height);

    gameButton1.frame = CGRectMake((703 - gameButton1.frame.size.width) /2, 345, gameButton1.frame.size.width, gameButton1.frame.size.height);
    gameButton2.frame = CGRectMake((703 - gameButton2.frame.size.width) /2, 391, gameButton2.frame.size.width, gameButton2.frame.size.height);
    gameButton3.frame = CGRectMake((703 - gameButton3.frame.size.width) /2, 437, gameButton3.frame.size.width, gameButton3.frame.size.height);
    gameButton4.frame = CGRectMake((703 - gameButton4.frame.size.width) /2, 483, gameButton4.frame.size.width, gameButton4.frame.size.height);
    gameButton5.frame = CGRectMake((703 - gameButton5.frame.size.width) /2, 529, gameButton5.frame.size.width, gameButton5.frame.size.height);
    label1.frame = CGRectMake((703-label1.frame.size.width)/2, 578, label1.frame.size.width, label1.frame.size.height);
    
    bildView.frame= CGRectMake((703 - bildView.frame.size.width - 10)/2 ,120 , bildView.frame.size.width, bildView.frame.size.height);;
    buttonNewWord.frame = CGRectMake((703-buttonNewWord.frame.size.width)/2,609 , buttonNewWord.frame.size.width, buttonNewWord.frame.size.height);
    deLabel.frame = CGRectMake((703-deLabel.frame.size.width)/2,577 , deLabel.frame.size.width, deLabel.frame.size.height);
    playButtonB1.frame = CGRectMake((703-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
    playButtonB2.frame = CGRectMake((703-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
    frLabel.frame = CGRectMake((703-frLabel.frame.size.width)/2,132 , frLabel.frame.size.width, frLabel.frame.size.height);
    richtigLabel.frame = CGRectMake((703-richtigLabel.frame.size.width)/2,90 , richtigLabel.frame.size.width, richtigLabel.frame.size.height);
    falschLabel.frame = CGRectMake((703 - falschLabel.frame.size.width - 10)/2 ,90 , falschLabel.frame.size.width, falschLabel.frame.size.height);


}

//___END_NGUYEN_HUY_SON__________________________________________________________________________________________

@end
